package com.dw.article.controller;



import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.shaded.com.google.common.collect.Lists;
import com.dw.article.mapper.ee.EeCommentInfoMapper;
import com.dw.article.service.CommentInfoService;
import com.dw.article.utils.LoginUtil;
import com.dw.common.entitys.BaseResponse;
import com.dw.common.enums.ErrorCode;
import com.dw.common.utils.ResultUtils;
import com.dw.model.article.dtos.comment.AddCommentDTO;
import com.dw.model.article.dtos.comment.QueryCommentDTO;
import com.dw.model.article.entitys.ArticleInfo;
import com.dw.model.article.entitys.CommentInfo;
import com.dw.model.article.vos.comment.QueryCommentVO;
import lombok.extern.slf4j.Slf4j;
import org.dromara.easyes.core.conditions.select.LambdaEsQueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 评论信息表(CommentInfo)表控制层
 *
 * @author makejava
 * @since 2024-10-14 16:30:58
 */
@Slf4j
@RestController
@RequestMapping("/article/comment")
public class CommentInfoController {

    @Resource
    private CommentInfoService commentInfoService;

    @Resource
    private EeCommentInfoMapper eeCommentInfoMapper;

    @GetMapping("/createIndex")
    public Boolean createIndex() {
        // 1.初始化-> 创建索引(相当于mysql中的表)
        return eeCommentInfoMapper.createIndex("comment_info");
    }

    @PostMapping("/add")
    public BaseResponse<Boolean> add(@RequestBody AddCommentDTO addCommentDTO) {
        log.info("新增评论参数:{}", addCommentDTO);
        // 参数拷贝
        CommentInfo commentInfo = new CommentInfo();
        BeanUtils.copyProperties(addCommentDTO, commentInfo);
        // 设置评论id
        long leiId = IdUtil.getSnowflake().nextId();
        commentInfo.setLeiId(leiId);
        // 设置创建人id
        String loginId = LoginUtil.getLoginId();
        commentInfo.setCreatedBy(Long.valueOf(loginId));
        // 将评论基本信息存入数据库
        CommentInfo mpCommentInfo = new CommentInfo();
        mpCommentInfo.setCreatedTime(DateTime.now());
        mpCommentInfo.setLeiId(leiId);
        mpCommentInfo.setArticleId(addCommentDTO.getArticleId());
        commentInfoService.save(mpCommentInfo);
        // ee新增评论
        Integer insert = eeCommentInfoMapper.insert(commentInfo);
        return ResultUtils.success(insert > 0);
    }

    /**
     * 根据文章id查询当前文章下的评论
     */
    @PostMapping("/query")
    public BaseResponse<List<QueryCommentVO>> queryComment(@RequestBody QueryCommentDTO queryCommentDTO) {
        try {
            if(log.isInfoEnabled()){
                log.info("CommentInfoController queryComment queryCommentDTO:{}", JSON.toJSONString(queryCommentDTO));
            }
            // ee查询当前文章下的所有评论
            LambdaEsQueryWrapper<CommentInfo> wrapper = new LambdaEsQueryWrapper<>();
            wrapper.eq(CommentInfo::getArticleId, queryCommentDTO.getArticleId());
            List<CommentInfo> commentInfos = eeCommentInfoMapper.selectList(wrapper);
            // 分页查询
            List<List<CommentInfo>> partition = Lists.partition(commentInfos, queryCommentDTO.getPageSize());
            List<CommentInfo> commentInfoList = partition.get(queryCommentDTO.getCurrent() - 1);
            // 中级查询
            List<QueryCommentVO> queryCommentVOList = commentInfoService.queryComment(commentInfoList);
            return ResultUtils.success(queryCommentVOList);
        } catch (Exception e){
            log.error("CommentInfoController queryComment error:{}",e.getMessage(),e);
            return ResultUtils.error(ErrorCode.SYSTEM_ERROR,"根据文章id查询当前文章下的评论报错");
        }
    }

}

